/*
 * @Author: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @Date: 2025-05-26 14:13:35
 * @LastEditors: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @LastEditTime: 2025-10-28 08:37:49
 * @FilePath: \express\express-demo\app.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const createError = require('http-errors');
const express = require('express');
const cors = require('cors');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const swaggerUI = require('swagger-ui-express');

const session = require('express-session');
const RedisStore = require('connect-redis').default;
// require('./config/redis');
require('./config/data');

const swaggerSpec = require('./swagger');
const { verifyToken } = require('@/middleWares/auth/jwt');
const auditLogMiddleware = require('@/middleWares/audit-log.middleware');
const responseFormatter = require('@/middleWares/response');
const indexRouter = require('@/routes/index');
// 管理端路由
const { adminAuthRouter, courseRouter, communityRouter, permissionRouter, productRouter, reportRouter, sportRouter, sysRouter, userRouter } = require('@/routes/admin');
// 用户端路由



const app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(swaggerSpec));
app.use(responseFormatter);
app.use(express.static(path.join(__dirname, 'public')));
// 访问静态图片
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
app.use('/', indexRouter);

// app.use(session({
//   store: new RedisStore({ client: redisClient }), // 使用 Redis 作为存储
//   secret: process.env.SESSION_SECRET || 'keep-motion',
//   resave: false, // 避免不必要的保存
//   saveUninitialized: false, // 符合 GDPR 规定
//   cookie: {
//     secure: false, // 在生产环境中设置为 true (仅 HTTPS)
//     maxAge: 1000 * 60 * 60 * 24 // 例如 1 天
//   }
// }));

// 路由划分
//  管理端根路由： /adminApi/v1
//  用户端根由:    /userApi/v1
// 无需登录校验的路由
app.use('/adminApi/v1/auth', adminAuthRouter);
// app.use('/userApi/v1/auth', userAuthRouter);

// 管理端路由
app.use('/adminApi/v1/permission', verifyToken, auditLogMiddleware, permissionRouter);
app.use('/adminApi/v1/user', verifyToken, auditLogMiddleware, userRouter);
app.use('/adminApi/v1/sys', verifyToken, auditLogMiddleware, sysRouter);
app.use('/adminApi/v1/sport', verifyToken, auditLogMiddleware, sportRouter);
app.use('/adminApi/v1/course', verifyToken, auditLogMiddleware, courseRouter);
app.use('/adminApi/v1/community', verifyToken, auditLogMiddleware, communityRouter);
app.use('/adminApi/v1/product', verifyToken, auditLogMiddleware, productRouter);
app.use('/adminApi/v1/report', verifyToken, auditLogMiddleware, reportRouter);

// 用户端路由




// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
